package com.fpx.order.plat.bussiness.plat.base;

import org.apache.log4j.Logger;

import com.amazonservices.mws.orders.MarketplaceWebServiceOrdersException;
import com.fpx.api.bean.request.SubmitOrderRequest;
import com.fpx.api.bean.response.SubmitOrderResponse;
import com.fpx.api.service.IAmazonService;
import com.fpx.api.service.IEbayService;
import com.fpx.api.service.ISmtService;
import com.fpx.api.util.Constant;
import com.fpx.order.plat.bussiness.system.PlatConstants;

/**
 * 运单回传服务
 * @createtime 2015年1月10日 上午9:21:43
 * @author jonsen
 */
public class SdkCommonOrderDeliverThread implements Runnable{
	
	private final static Logger LOGGER = Logger.getLogger(SdkCommonOrderDeliverThread.class);
	
	/**超时时间(timeout*5)秒,默认3分钟*/
	private int timeout = 36;
	private SubmitOrderRequest  request;
	private SubmitOrderResponse  response;
	/**渠道*/
	private String platformCode;
	
	private IEbayService ebayService;
	
	private IAmazonService amazonService;
	
	private ISmtService smtService;
	
	private boolean flag = false;
	
	public SdkCommonOrderDeliverThread(int timeout,SubmitOrderRequest request,String platformCode,Object obj){
		if(timeout>36){
			this.timeout = timeout;
		}
		this.request = request;
		this.platformCode = platformCode;
		
		if (obj instanceof IEbayService) {
			this.ebayService = (IEbayService) obj;
		} else if (obj instanceof IAmazonService) {
			this.amazonService = (IAmazonService) obj;
		} else if (obj instanceof ISmtService) {
			this.smtService = (ISmtService) obj;
		}
	}
	
	@Override
	public void run() {
		if(platformCode.equals(PlatConstants.ORDER_SOURCE_EBAY)){
			try {
				response = ebayService.submitOrder(request);
				// 只有请求成功
				flag = true;
			} catch (Exception e) {
				LOGGER.error("【ebay】--调用订单回传业务类时异常---异常信息：\n"+e.getMessage());
				flag = true;
				response = new SubmitOrderResponse();
				response.setAck(Constant.ACK_FAIL);
				response.setFailMessage(e.getMessage());
			}
		}else if(platformCode.equals(PlatConstants.ORDER_SOURCE_AM)){
			recuAmzoneSubmit(2);
		}else if(platformCode.equals(PlatConstants.ORDER_SOURCE_SMT)){
			try {
				response = smtService.submitOrder(request);
				flag = true;
			} catch (Exception e) {
				LOGGER.error("【smt】--调用订单回传业务类时异常---异常信息：\n"+e.getMessage());
				flag = true;
				response = new SubmitOrderResponse();
				response.setAck(Constant.ACK_FAIL);
				response.setFailMessage(e.getMessage());
			}
		}
	}
	
	 public void recuAmzoneSubmit(int a){
		try {
			if(a>0){
				response=amazonService.submitOrder(request);
			}
			// 只有请求成功或请求次数上限才为true
			flag = true;
		} catch (MarketplaceWebServiceOrdersException e) {
			response = new SubmitOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());	
			--a;
			if(a==0){
				LOGGER.error("amazon 调用订单回传业务类时MarketplaceWebServiceOrder异常---异常信息：\n"+e.getMessage());	
			}
			recuAmzoneSubmit(a);//如果发生网络相关的异常就多调用一次
		} catch (Exception e) {
			response = new SubmitOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());	
			recuAmzoneSubmit(0);
		}
		 
		 
	}
	
	
	/**
	 * 超时时间 5*12=60秒
	 * @createtime 2015年1月10日 上午9:33:12
	 * @author jonsen
	 */
	public SubmitOrderResponse getResponse() {
		int i=0;
		while(i<timeout){
			if(this.response == null||!flag){
				try {
					Thread.sleep(5000);
				}catch (InterruptedException e) {
					LOGGER.error("等待超时 出现异常 ---异常信息："+e.getMessage());
				}
			}else{
				break;
			}
			i++;
		}
		return response;
	}
}
